package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseTableListAdapter;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.EmployeeLoanContent;
import com.isunland.managesystem.entity.TravelListContent;
import com.isunland.managesystem.entity.TravelListContentOriginal;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class ManageReimburseListFragment extends BaseListFragment {
  public static final String EXTRA_TRAVELLIST_CONTENT =
      "com.isunland.managesystem.ui.EXTRA_EXTRA_TRAVELREIMBURSE_CONTENT";
  private static final int REQUEST_ADD = 0;
  private static final int REQUEST_DETAIL = 1;
  private CurrentUser mCurrentUser;
  private EmployeeLoanContent mEmployeeLoanContent;
  private BaseTableListAdapter mAdapter;
  private ArrayList<TravelListContent> mTravelListContents;// 解析列表集合
  private String mDataStatus;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCurrentUser = CurrentUser.newInstance(getActivity());
    mEmployeeLoanContent =
        (EmployeeLoanContent) getArguments().getSerializable(EXTRA_TRAVELLIST_CONTENT);
    mDataStatus = mEmployeeLoanContent.getDataStatus();

  }

  //菜单按钮
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        if ("new".equals(mDataStatus) || "abort".equals(mDataStatus)) {
          // AddManageReimburseListActivity管理明细添加、修改
          Intent i = new Intent(getActivity(), AddManageReimburseListActivity.class);
          i.putExtra(AddManageReimburseListActivity.EXTRA_TYPE,
              AddManageReimburseListActivity.EXTRA_ADDCONTENTTYPE);
          i.putExtra(AddManageReimburseListFragment.EXTRA_TRAVELREIMBURSE_CONTENT,
              mEmployeeLoanContent);
          startActivityForResult(i, REQUEST_ADD);
        } else {
          Toast.makeText(getActivity(), R.string.draftOrAbort, Toast.LENGTH_SHORT).show();
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.URL_TRAVEL_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("curPage", 1 + "");
    params.put("pageSize", "20");
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("tableName", "imsoa.manage_fee_out");
    params.put("mainId", mEmployeeLoanContent.getId());
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    TravelListContentOriginal original = new Gson().fromJson(arg0, TravelListContentOriginal.class);
    List<TravelListContent> content = original.getRows();
    setListAdapter(content);
  }

  private void setListAdapter(List<TravelListContent> content) {
    if (mTravelListContents == null) {
      mTravelListContents = new ArrayList<>();
    }
    // 不为空
    mTravelListContents.clear();
    mTravelListContents.addAll(content);
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    map.put("费用分类", "FEE_KIND");
    map.put("发生日期", "FEE_DATE");
    map.put("费用小计", "FEE_AMOUNT");
    if (mAdapter == null) {
      mAdapter = new BaseTableListAdapter(getActivity(), mTravelListContents, map,
          new TravelListContent());
      setListAdapter(mAdapter);
    }
    ((BaseTableListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  public void refresh() {
    volleyPost();
  }

  //点击差旅list跳转到详情
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    TravelListContent mconten = mTravelListContents.get(position - 1);
    LogUtil.i(mconten.toString());
    Intent i = new Intent(getActivity(), AddManageReimburseListActivity.class);
    i.putExtra(ShowTravelReimburseListDetailFragment.EXTRA_TRAVELLIST_CONTENT, mconten);
    i.putExtra(ShowTravelReimburseListDetailFragment.EXTRA_EMPLOYEELOAN_CONTENT,
        mEmployeeLoanContent);
    i.putExtra(AddManageReimburseListActivity.EXTRA_TYPE,
        AddManageReimburseListActivity.EXTRA_SHOWCONTENTTYPE);//type
    startActivityForResult(i, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_ADD) {
      volleyPost();

      return;
    }
    if (requestCode == REQUEST_DETAIL) {
      volleyPost();

      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  // fragment之间传值
  public static ManageReimburseListFragment newInstance(EmployeeLoanContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_TRAVELLIST_CONTENT, item);
    ManageReimburseListFragment fragment = new ManageReimburseListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
